local cjson = require "cjson"
local auth = require "auth"

local db = auth.connect_to_mysql()
if not db then
    ngx.status = 500
    ngx.say(cjson.encode({success = false, message = "Database connection failed"}))
    return
end

local sql = "SELECT id, username FROM users"
local res, err, errno, sqlstate = db:query(sql)

if not res then
    ngx.log(ngx.ERR, "failed to fetch users: ", err)
    ngx.status = 500
    ngx.say(cjson.encode({success = false, message = "Failed to fetch users"}))
else
    ngx.say(cjson.encode({success = true, users = res}))
end

db:close()
